#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/config/SourcePrefix.h>
#include "pullsdk.h"
#include "platform.h"
#include "udktest_pullapi.h"

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( UDKPullApiTest );
static const char* vaid_tcp_connstring = "protocol=TCP,Address=192.168.0.125,port=4370,timeout=4000,passwd=";
void UDKPullApiTest::testConnect(){
  char commbuf[1024];  
  //invalid connect  
  CPPUNIT_ASSERT(PullSDKConnect(NULL)==NULL);
  CPPUNIT_ASSERT(PullSDKConnect("Address=192.168.1.201")==0);
  CPPUNIT_ASSERT(PullSDKConnect("Address=192.168.1.201,port=1234")==0);
  CPPUNIT_ASSERT(PullSDKConnect("Protocol=TCP,Address=192.168.1.201,port=1234")==0);
  CPPUNIT_ASSERT(PullSDKConnect("protocol=TCP,Address=192.168.1.201,port=1234")==0);
  CPPUNIT_ASSERT(PullSDKConnect("protocol=TCP,Address=192.168.1.1,port=80")==0);
  CPPUNIT_ASSERT(PullSDKConnect("protocol=TCP,Address=255.255.255.255,port=80")==0);
  CPPUNIT_ASSERT(PullSDKConnect("protocol=TCP,Address=192.168.1.255,port=80")==0);
  CPPUNIT_ASSERT(PullSDKConnect("protocol=TCP,Address=192.168.1.0,port=80")==0);  
  CPPUNIT_ASSERT(PullSDKGetDeviceParam((UDK_HANDLE)0x0,commbuf,64,"FirmVer")!=0); 

  /*
   * Following cases will cause segmention fault
  CPPUNIT_ASSERT(PullSDKGetDeviceParam((UDK_HANDLE)0x12345678,commbuf,64,"FirmVer")!=NULL);
  CPPUNIT_ASSERT(PullSDKGetDeviceParam((UDK_HANDLE)-1,commbuf,64,"FirmVer")!=NULL);
  */
}

void UDKPullApiTest::testConnectTCP(){
  UDK_HANDLE h;
  int ret;
  char commbuf[64]={0};
  char* buffer;
  h = PullSDKConnect(vaid_tcp_connstring);
  CPPUNIT_ASSERT(h!=NULL);
  CPPUNIT_ASSERT_EQUAL(PullSDKGetDeviceParam(h,commbuf, 64, "FirmVer"),0);

  CPPUNIT_ASSERT_EQUAL(PullSDKSetDeviceParam(h, "Device=1,Door1SensorType=2,Door1Drivertime=5,Door1Intertime=4"),0);
  CPPUNIT_ASSERT_EQUAL(PullSDKControlDevice(h, 1, 1, 2, 6, 0, NULL),0);
  CPPUNIT_ASSERT_EQUAL(PullSDKSetDeviceData(h, "user", "Pin=19999\tCardNo=13375401\tPassword=1\r\nPin=2\tCardNo=14128058\tPassword=1", NULL),0);
  CPPUNIT_ASSERT(PullSDKGetDeviceDataCount(h, "user", "*", NULL)!=0);
  CPPUNIT_ASSERT(PullSDKDeleteDeviceData(h, "user", "Pin=19999", NULL)==0);
  char* rtlog_buffer = (char*)udk_malloc(4096);
  CPPUNIT_ASSERT(rtlog_buffer!=NULL);
  memset(rtlog_buffer,0,sizeof rtlog_buffer);
  CPPUNIT_ASSERT(PullSDKGetRTLog(h, rtlog_buffer, 4096)!=0);
  udk_free(rtlog_buffer);
  PullSDKDisconnect(h);

  h = PullSDKConnect(vaid_tcp_connstring);
  CPPUNIT_ASSERT(h!=NULL);
  if(PullSDKGetDeviceParam(h,buffer, 64, "FirmVer")==0){
	CPPUNIT_FAIL("invalid buffer should return error\n");
  }
  buffer = (char*)udk_malloc(64);
  CPPUNIT_ASSERT(buffer!=NULL);
  if(PullSDKGetDeviceParam(h,buffer, 64, "FirmVer")!=0){
	CPPUNIT_FAIL("valid buffer should not return error\n");
  }
  PullSDKDisconnect(h);  
  if(PullSDKGetDeviceParam(h,buffer, 64, "FirmVer")==0){
	CPPUNIT_FAIL("invalid PullSDK handle should return error\n");
  }
  //
  //start multi connection
  //
  UDK_HANDLE handles[10]={NULL};
  int i;
  for(i=0;i<10;i++){
    handles[i] = PullSDKConnect(vaid_tcp_connstring);
    CPPUNIT_ASSERT(handles[i]!=NULL);
    if(PullSDKGetDeviceParam(handles[i],buffer, 64, "FirmVer")!=0){
	CPPUNIT_FAIL("invalid PullSDK handle should return error\n");
    }
  }
  for(i=0;i<10;i++){
    PullSDKDisconnect(handles[i]);
  }
  udk_free(buffer);
}

void UDKPullApiTest::testPullSDKGetDeviceDataTCP(){

}
void UDKPullApiTest::testPullSDKSetDeviceDataTCP()
{

}
void UDKPullApiTest::testPullSDKDeleteDeviceDataTCP()
{
}
void UDKPullApiTest::testPullSDKGetDeviceDataCountTCP()
{
	
}
void UDKPullApiTest::testPullSDKGetDeviceParamTCP()
{
	
}
void UDKPullApiTest::testPullSDKSetDeviceParamTCP()
{
	
}
void UDKPullApiTest::testPullSDKGetDeviceFileDataTCP()
{
	int ret;
	char* buf=NULL;
	int buflen=0;
	UDK_HANDLE h = PullSDKConnect(vaid_tcp_connstring);
	CPPUNIT_ASSERT(h!=NULL);
	ret = PullSDKGetDeviceFileData(h,buf,&buflen,"main",NULL);
	CPPUNIT_ASSERT(ret!=0);
	buf = (char*)udk_malloc(5*1024*1024);
	CPPUNIT_ASSERT(buf!=NULL);
	buflen = 5*1024*1024;
	ret = PullSDKGetDeviceFileData(h,buf,&buflen,"main",NULL);
	CPPUNIT_ASSERT(ret>=0);
	buflen=0;
	ret = PullSDKGetDeviceFileData(h,buf,&buflen,"main",NULL);
	CPPUNIT_ASSERT(ret!=0);
	PullSDKDisconnect(h);
	udk_free(buf);
}
void UDKPullApiTest::testPullSDKSetDeviceFileDataTCP()
{
	

}
void UDKPullApiTest::testPullSDKGetRTLogTCP()
{
	int ret;
	char* buf=NULL;
	int buflen=0;
	UDK_HANDLE h = PullSDKConnect(vaid_tcp_connstring);
	CPPUNIT_ASSERT(h!=NULL);
	ret = PullSDKGetRTLog(h,buf,buflen);
	CPPUNIT_ASSERT(ret!=0);
	buf = (char*)udk_malloc(128*1024);
	CPPUNIT_ASSERT(buf!=NULL);
	buflen = 128*1024;
	ret = PullSDKGetRTLog(h,buf,buflen);
	CPPUNIT_ASSERT(ret>=0);
	buflen=0;
	ret = PullSDKGetRTLog(h,buf,buflen);
	CPPUNIT_ASSERT(ret!=0);
	PullSDKDisconnect(h);
	udk_free(buf);
}
void UDKPullApiTest::testPullSDKSearchDevice()
{
	int bufsize;
	int ret;
	char* searchbuf = (char*)udk_malloc(128*1024);
	bufsize = 128*1024;
	ret = PullSDKSearchDevice((char*)"UDP",(char*)"255.255.255.255",searchbuf,&bufsize);
	CPPUNIT_ASSERT(ret>=0);	
	bufsize = 128*1024;
	//invalid subnet 
	ret = PullSDKSearchDevice((char*)"UDP",(char*)"255.0.0.0",searchbuf,&bufsize);
	CPPUNIT_ASSERT(ret<=0);
	bufsize = 128*1024;
	ret = PullSDKSearchDevice((char*)"UDP",(char*)"255.255.0.0",searchbuf,&bufsize);
	CPPUNIT_ASSERT(ret<=0);
	bufsize = 128*1024;
	ret = PullSDKSearchDevice((char*)"UDP",(char*)"224.0.0.0",searchbuf,&bufsize);
	CPPUNIT_ASSERT(ret<=0);	
	//valid subnet
	bufsize = 128*1024;
	ret = PullSDKSearchDevice((char*)"UDP",(char*)"192.168.0.255",searchbuf,&bufsize);
	CPPUNIT_ASSERT(ret>=0);
	udk_free(searchbuf);
}
void UDKPullApiTest::testPullSDKModifyIPAddress()
{
	
}

void UDKPullApiTest::testConnectUDP(){

}
void UDKPullApiTest::testConnectP4P(){

}


